package com.wsoft.hr.pm.manager;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wsoft.core.utils.LoginUserUtil;
import com.wsoft.entity.BaseEntity;
import com.wsoft.hr.pm.entity.HrPmMemberTemplateEntity;
import com.wsoft.hr.pm.mapper.HrPmMemberTemplateMapper;
import com.wsoft.hr.pm.query.activity.member.template.MemberTemplateListQuery;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @Author zgk
 * @Date 2025年02月17日 15:06:35
 */
@Slf4j
@Service
public class HrPmMemberTemplateManager extends ServiceImpl<HrPmMemberTemplateMapper, HrPmMemberTemplateEntity> {

    /**
     * 预警获取数据, 获取时间区间范围内的数据
     * @param beginTime 开始时间
     * @param endTime 结束时间啊in
     * @return
     */
    public List<HrPmMemberTemplateEntity> getWarningList(String beginTime,String endTime){
        QueryWrapper<HrPmMemberTemplateEntity> query = new QueryWrapper<>();
        query.lambda().eq(HrPmMemberTemplateEntity::getScoreStatus, 2)
                .ge(StrUtil.isNotBlank(beginTime), BaseEntity::getUpdateTime, beginTime)
                .le(StrUtil.isNotBlank(endTime), BaseEntity::getUpdateTime, endTime);
        return list(query);
    }

    /**
     * 根据活动查询
     * @param activityIds
     * @param status ==> 查询的targetStatus
     * @return
     */
    public List<HrPmMemberTemplateEntity> listByActivityIds(List<Long> activityIds,Integer status){
        QueryWrapper<HrPmMemberTemplateEntity> query = new QueryWrapper<>();
        query.lambda().in(CollUtil.isNotEmpty(activityIds), HrPmMemberTemplateEntity::getActivityId, activityIds)
                .eq(status != null, HrPmMemberTemplateEntity::getTargetStatus, status);
        return list(query);
    }


    public List<HrPmMemberTemplateEntity> getMemberTemplateByQuery(Long activityId, Long memberId) {
        QueryWrapper<HrPmMemberTemplateEntity> query = new QueryWrapper<>();
        query.lambda().eq(activityId != null, HrPmMemberTemplateEntity::getActivityId, activityId)
                .eq(memberId != null, HrPmMemberTemplateEntity::getMemberId, memberId);
        return list(query);
    }

    public List<HrPmMemberTemplateEntity> list(MemberTemplateListQuery query) {
        QueryWrapper<HrPmMemberTemplateEntity> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().eq(HrPmMemberTemplateEntity::getActivityId, query.getActivityId())
                .like(StrUtil.isNotBlank(query.getName()), HrPmMemberTemplateEntity::getMemberName, query.getName());
        return super.list(queryWrapper);
    }

    public List<HrPmMemberTemplateEntity> listByActivityIds(List<Long> activityIds) {
        QueryWrapper<HrPmMemberTemplateEntity> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().in(HrPmMemberTemplateEntity::getActivityId, activityIds)
                .eq(HrPmMemberTemplateEntity::getMemberId, LoginUserUtil.getUserInfo().getLoginUserVO().getId());
        return list(queryWrapper);
    }

    public HrPmMemberTemplateEntity getScoreByBusinessKey(String businessKey) {
        QueryWrapper<HrPmMemberTemplateEntity> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().eq(HrPmMemberTemplateEntity::getScoreBusinessCode, businessKey);
        return getOne(queryWrapper);
    }

    public HrPmMemberTemplateEntity getTargetByBusinessKey(String businessKey) {
        QueryWrapper<HrPmMemberTemplateEntity> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().eq(HrPmMemberTemplateEntity::getTargetBusinessCode, businessKey);
        return getOne(queryWrapper);
    }


}
